package com.roncoo.education.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Service
public class FileService {
    @Value("${file.upload-dir}")
    private String uploadDir;

    public boolean deleteFile(String filePath) {
        try {
            Path path = Paths.get(filePath);
            if (Files.exists(path)) {
                Files.delete(path);
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteFileByUrl(String fileUrl) {
        // 假设文件存储在项目的 uploads 目录下
        String basePath = uploadDir;
        String filePath = basePath + fileUrl;
        return deleteFile(filePath);
    }
}
